//
//  RYTijiaoViewController.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/13.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import SDCycleScrollView

class RYTijiaoViewController: SYBaseViewController {

    
    /*
     "id"
         "remainNum"
         "purchaseLimit"

     */
    var params: [String: String] = [:]
    var model: RYProductDetailModel!
    var maxNum = 0
    let tijiao = RYTijiaoView()
    
    
    let back: VGImageButton = {
        let v: VGImageButton  = VGImageButton()
        v.imageView.image = UIImage(named: "nav_back")
        return v
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    
    init(params: RYProductDetailModel) {
        super.init(nibName: nil, bundle: nil)
        
        self.params = ["id": "\(params.id ?? "")", "remainNum": "\(params.remainNum ?? 0)", "purchaseLimit": "\(params.purchaseLimit ?? 0)"]
        self.model = params
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        cycleView()
        self.getDetailData()
    }

    func getDetailData() {
        requestVM.baseRequest(disposeBag: dispose, type: .nongchangProductBuyMaxNum(dic: self.params), modelClass: SGBaseModel<RYBuyModel>.self) {[weak self] (res) in
            if let `self` = self {
                let model:RYBuyModel = res.data ?? RYBuyModel()
                
                self.maxNum = model.maxBuyNum ?? 0
                
            }
            
        } Error: {
            
        }
        
    }
    
    func cycleView() {
        
        let cycleScrollView: SDCycleScrollView = SDCycleScrollView(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 280.0.autoHeihgt()))
        cycleScrollView.delegate = self
        cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated
        cycleScrollView.autoScrollTimeInterval = 2// 自动滚动时间间隔
        cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        cycleScrollView.pageControlBottomOffset = -30
        cycleScrollView.pageDotColor = .gray
        cycleScrollView.currentPageDotColor = .blue
        cycleScrollView.backgroundColor = .gray
        // 添加到view
        self.view.addSubview(cycleScrollView)
        
        let urls:[String] = model.carouselImgUrl?.components(separatedBy: ",") ?? []
        var arr:[String] = []
        for (_,value) in urls.enumerated() {
            arr.append("\(IP)\(value)")
        }
        cycleScrollView.imageURLStringsGroup = arr
        
        self.view.addSubview(back)
        back.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(STATUSBAR_HEIGHT)
            make.height.equalTo(48.autoWidth())
            make.width.equalTo(38.autoWidth())
        }
        
        back.btn.addAction {
            self.navigationController?.popViewController(animated: true)
        }
        
        setupBottom()
    }
    
    func setupBottom() {
        tijiao.layer.cornerRadius = 10
        tijiao.currentVC = self
        self.tijiao.configure(model)
        view.addSubview(tijiao)
        tijiao.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(20.autoWidth())
            make.height.equalTo(418.autoHeihgt())
        }
    }
    

}

extension RYTijiaoViewController: SDCycleScrollViewDelegate {
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        print("选择了轮播图\(index)")
    }
}
